package com.luoxiang.socialandroid.fragment;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.activity.UserInfoActivity;
import com.luoxiang.socialandroid.adapter.SendCoinAdapter;
import com.luoxiang.socialandroid.base.LazyLoadFragment;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.bean.QingQiuBean;
import com.luoxiang.socialandroid.bean.SendCoinBean;
import com.luoxiang.socialandroid.dialog.TipDialog;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils;
import com.lxj.xpopup.XPopup;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;

/**
 * @Description TODO
 * @systemUser Administrator
 * @Author LX
 * @Date 09-21-2022 周三 15:22
 */
public class LiveCoinFragment extends LazyLoadFragment {
    private String token;
    private RecyclerView rv_licecoin;
    private SendCoinAdapter adapter;

    @Override
    protected int setContentView() {
        return R.layout.fragment_duihuan;
    }

    @Override
    protected void initView() {
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        getRequest();
        getExchangePackages(EXTRA, token);
        rv_licecoin = findViewById(R.id.rv_licecoin);
    }

    /**
     * 金币列表接口
     *
     * @param extra
     * @param token
     */
    private void getExchangePackages(String extra, String token) {
        NetUtils.getInstance().getApiService().getExchangePackages(extra, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        com.blankj.utilcode.util.LogUtils.i("decryptStr299", decryptStr);
                        Gson gson = new Gson();
                        SendCoinBean sendCoinBean = gson.fromJson(decryptStr, SendCoinBean.class);
                        adapter = new SendCoinAdapter(sendCoinBean.getData().getExchanges());
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                        rv_licecoin.setLayoutManager(linearLayoutManager);
                        rv_licecoin.setAdapter(adapter);
                        adapter.setOnAddClickListener(new SendCoinAdapter.OnAddClickListener() {
                            @Override
                            public void onItemClicks(int position, int packageId) {
                                TipDialog tipDialog = new TipDialog(getActivity());
                                tipDialog.setTitle(getString(R.string.mi_caozuotishi));
                                tipDialog.setCancel(getString(R.string.mi_quxiao));
                                tipDialog.setContent(getString(R.string.mi_shifouxiaohaojifenduihaunjinbi));
                                tipDialog.setConfirm(getString(R.string.mi_queding));
                                tipDialog.setOnCancelClickListener(new Function0<Unit>() {
                                    @Override
                                    public Unit invoke() {
                                        tipDialog.dismiss();
                                        return null;
                                    }
                                });
                                tipDialog.setOnConfirmClickListener(new Function0<Unit>() {
                                    @Override
                                    public Unit invoke() {
                                        getGoexchangePackage(EXTRA, token, packageId);
                                        tipDialog.dismiss();
                                        return null;
                                    }
                                });
                                new XPopup.Builder(getActivity())
                                        .asCustom(tipDialog)
                                        .show();

                            }
                        });

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 请求消费积分接口
     *
     * @param extra
     * @param token
     * @param packageId
     */
    private void getGoexchangePackage(String extra, String token, int packageId) {
        NetUtils.getInstance().getApiService().getGoexchangePackage(extra, token, packageId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        com.blankj.utilcode.util.LogUtils.i("decryptStr399", decryptStr);
                        Gson gson = new Gson();
                        QingQiuBean qingQiuBean = gson.fromJson(decryptStr, QingQiuBean.class);
                        if (qingQiuBean.getCode() == 0) {
                            ToastUtils.showShort(R.string.mi_duihuanzhong);
                        } else {
                            ToastUtils.showShort(qingQiuBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    @Override
    protected void lazyLoad() {

    }
}
